package com.itheima.app.controller;


import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.MakeFriendManager;
import com.itheima.domain.mongo.Visitor;
import com.itheima.domain.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MakeFriendController {

    @Autowired
    private MakeFriendManager makeFriendManager;

    /**
     * 查询今日佳人
     */
    @GetMapping("/tanhua/todayBest")
    public RecommendUserVo findRecommendUserByUserId(){
        return makeFriendManager.findRecommendUserByUserId();
    }

    /**
     * 查询推荐好友
     */
    @GetMapping("/tanhua/recommendation")
    public PageBeanVo findRecommendUser(
            @RequestParam(defaultValue = "1" , name = "page") Integer pageNumber ,
            @RequestParam(defaultValue = "10", name = "pagesize" )Integer pageSize
    ){
        return makeFriendManager.findRecommendUser(pageNumber ,pageSize );
    }


    /**
     * 探花查询卡片
     */
    @GetMapping("/tanhua/cards")
    public List<RecommendUserVo> findTanhuaCards(
            @RequestParam(defaultValue = "1" , name = "page") Integer pageNumber ,
            @RequestParam(defaultValue = "10", name = "pagesize" )Integer pageSize
    ){
        //查询推荐的好友信息
        PageBeanVo pageBeanVo = makeFriendManager.findRecommendUser(1,20);
        return (List<RecommendUserVo>) pageBeanVo.getItems();
    }

    /**
     * 查询佳人信息
     * @return
     */
    @GetMapping("/tanhua/{id}/personalInfo")
    public RecommendUserVo findPersonalInfo(
            @PathVariable("id") Long recommendUserId
    ){
        return makeFriendManager.findPersonalInfo(recommendUserId);
    }

    /**
     * 查询陌生人问题
     * @param userId
     * @return
     */
    @GetMapping("/tanhua/strangerQuestions")
    public String findStrangerQuestion( Long userId){

        return makeFriendManager.findStrangerQuestion(userId);
    }


    /**
     * 最近访客
     */
    @GetMapping("/movements/visitors")
    public List<VisitorVo> findVisitor(){

        return makeFriendManager.findVisitor();
    }

    /**
     * 上传位置
     * @return
     */
    @PostMapping("/baidu/location")
    public Map saveOrUpdateUserLocation(@RequestBody Map<String ,String> map){
        Double latitude = Double.valueOf(map.get("latitude"));
        Double longitude = Double.valueOf(map.get("longitude"));
        String addrStr = map.get("addrStr");
        //System.out.println("上传的位置:" + map);
        makeFriendManager.saveOrUpdateUserLocation(latitude , longitude , addrStr);
        return null;
    }

    /**
     * 搜附近
     * @return
     */
    @GetMapping("/tanhua/search")
    public List<NearUserVo> searchUser(String gender,String distance){

        return   makeFriendManager.searchUser(gender , distance );
    }

    /**
     * 用户认证
     *  客户端主动访问获得环信的账户
     */
    @GetMapping("/huanxin/user")
    public Map getHuanxinUser(){
        Map<String,String> map= new HashMap();
        map.put("username", UserHolder.getId().toString()); //此处必须是string类型 huanxin要string
        map.put("password","123456");
        System.out.println("用户认证:" + map);
        return map;
    }
    /**
     * 打招呼
     */
    @PostMapping("/tanhua/strangerQuestions")
    public Map replyStrangerQuestion(@RequestBody Map<String,String> map){
        String questionUserId = map.get("userId");
        String reply = map.get("reply");
        makeFriendManager.replyStrangerQuestion(questionUserId , reply);
        return null;
    }


    /**
     * 添加联系人
     * @param map
     * @return
     */
    @PostMapping("/messages/contacts")
    public Map saveContact(@RequestBody Map<String,String> map){
        String replyUserId = map.get("userId");
        makeFriendManager.saveContact(replyUserId);
        return null;
    }


    /**
     * 查询好友列表数据
     * @return
     */
    @GetMapping("/messages/contacts")
    public PageBeanVo findContact(
            @RequestParam(defaultValue = "1" , name = "page") Integer pageNumber ,
            @RequestParam(defaultValue = "10", name = "pagesize" )Integer pageSize
    ){

        return makeFriendManager.findContact(pageNumber , pageSize);
    }


    /**
     * 喜欢
     * @param userLikeId
     * @return
     */
    @GetMapping("/tanhua/{id}/love")
    public Map saveUserLike(
            @PathVariable("id") Long userLikeId

    ){
         makeFriendManager.saveUserLike(userLikeId);
         return null;
    }

    /**
     * 查看我的喜欢 粉丝 互相喜欢数量
     * @return
     */
    @GetMapping("/users/counts")
    public UserLikeCountVo usersCounts(){
         return  makeFriendManager.usersCounts();
    }

}
